﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Emoney.UserTraining.BLL;
using Emoney.UserTraining.Model;

namespace Emoney.UserTraining.Admin.Controllers
{
    public class LoginController : Controller
    {
        public ActionResult Index()
        {
            var t = Request["LoginOut"];
            if (!string.IsNullOrEmpty(t))
            {
                setCookie("webadmin", "adminname", "");
            }
            return View();
        }

        [HttpGet]
        public ActionResult Login()
        {
            return View("index");
        }

        [HttpPost]
        public ActionResult Login(string userName, string pwd)
        {
            Web_TrainAdminBLL bll = new Web_TrainAdminBLL();
            var result = bll.Login(userName, pwd);
            if (result == null)
            {
                ViewData["userName"] = userName;
                ViewData["error"] = "用户名或密码错误";
                return View("index");
            }
            else
            {
                setCookie("webadmin", "adminname", userName);
                return Redirect("~/login/Display");
            }
        }

        public ActionResult Display()
        {
            if (string.IsNullOrEmpty(getCookie("webadmin", "adminname")))
            {
                return Redirect("~/login/index");
            }
            return View();
        }

        public string getCookie(string cookieName, string keyguid)
        {
            if (Request.Cookies[cookieName] != null)
            {
                //读取Cookie 键值
                return Request.Cookies[cookieName][keyguid].ToString();
            }
            return "";
        }

        private void setCookie(string cookieName, string keyguid, string keyvalue)
        {
            //创建Cookie 并命名
            HttpCookie cookietwo = new HttpCookie(cookieName);
            //   cookietwo.Name = "UserInfo";
            //Cookie为一年有效期
            cookietwo.Expires = DateTime.Today.AddDays(1);
            //设置Cookie对应键值 
            cookietwo.Values[keyguid] = keyvalue;
            //  CookieHelp.CookieHelper.AddCookie(cookietwo);
            // 用Response添加到Cookie中
            Response.Cookies.Add(cookietwo);
        }

        public ActionResult menu()
        {
            List<Power> lst = new List<Power>();
            Power power1 = new Power();
            power1.Id = "01";
            power1.Name = "网络会场";
            power1.Url = "Temporary/index";
            power1.Pid = "";
            lst.Add(new Power() { Id = "01", Name = "网络会场", Url = "Temporary/index", Pid = "" });
            lst.Add(new Power() { Id = "0101", Name = "网络会场信息管理", Url = "Temporary/index", Pid = "01" });
            //aspx/
            lst.Add(new Power() { Id = "0102", Name = "网络会场信息发布", Url = "aspx/NetworkMeetingAdd.aspx", Pid = "01" });
            lst.Add(new Power() { Id = "0103", Name = "历史课程管理", Url = "Temporary/NetworkMeetingList", Pid = "01" });
            lst.Add(new Power() { Id = "0104", Name = "讲师管理", Url = "Temporary/TraningLecturerList", Pid = "01" });
            lst.Add(new Power() { Id = "0105", Name = "会议类型管理", Url = "Temporary/MeetingTypeList", Pid = "01" });

            ViewData["menu"] = lst;
            return View();
        }

        public ActionResult header()
        {
            string userName = getCookie("webadmin", "adminname");
            ViewData["loginname"] = userName;
            return View();
        }

        public ActionResult main()
        {
            string userName = getCookie("webadmin", "adminname");
            ViewData["loginname"] = userName;
            ViewData["role"] = "产品使用者";
            return View();
        }
    }
}
